#!/bin/bash

DST_LIB_FOLDER=/usr/lib/chopito
DST_BIN_FOLDER=/usr/sbin
DST_ETC_FOLDER=/etc/chopito

function do_uninstall()
{
	echo " * Uninstalling Chopito Service"
	# Remove source code
	rm -f $DST_BIN_FOLDER/chopito
	rm -Rf $DST_LIB_FOLDER

	# We keep config files
}

function do_install()
{
	echo " * Installing Chopito Service"
	# Copy configuration files
	[ ! -d $DST_ETC_FOLDER ] && mkdir -p $DST_ETC_FOLDER
	[ -f $DST_ETC_FOLDER/chopito.conf ] && mv $DST_ETC_FOLDER/chopito.conf $DST_ETC_FOLDER/chopito.conf.bak 
	cp ./etc/chopito.conf $DST_ETC_FOLDER/

	# Copy source code
	[ ! -d $DST_LIB_FOLDER ] && mkdir -p $DST_LIB_FOLDER
	cp -Rf ./src/lib/* $DST_LIB_FOLDER/
	ln -s $DST_LIB_FOLDER/chopito.py $DST_BIN_FOLDER/chopito
}

echo "Chopito Installer"

if [ "$(echo $@ | grep '\-\-install')" != "" ] || [ "$(echo $@ | grep '\-\-uninstall')" = "" ]
then
	if [ -d $DST_LIB_FOLDER ]
	then
		if [ "$(echo $@ | grep '\-\-force')" != "" ]
		then
			echo "Force install"
			do_uninstall
			do_install
			exit 0	
		else
			echo "Chopito is already installed. Use $0 --force to reinstall it."
			exit 1	
		fi
	else
		do_install
		exit 0
	fi
fi

if [ "$(echo $@ | grep '\-\-uninstall')" != "" ]
then
	do_uninstall
	exit 0
fi

